import {_decorator, Component, Label, Animation} from 'cc';
import {GameOverViewModel} from "db://assets/Scripts/MVC/game/over/GameOverViewModel";
import {GameOverUiState} from "db://assets/Scripts/MVC/game/over/GameOverUiState";
import {AriPlane} from "db://assets/Scripts/Common/AriPlane";
import {EventManager} from "db://assets/Scripts/Common/EventManager";
import {EventType} from "db://assets/Scripts/Common/EventType";
import {NodePath} from "db://assets/Scripts/Common/NodePath";
import {AudioManager} from "db://assets/Scripts/Common/AudioManager";
import {AudioPath} from "db://assets/Scripts/Common/AudioPath";

const {ccclass, property} = _decorator;

/**
 * 游戏结束面板
 */
@ccclass('GameOverController')
export class GameOverController extends Component {
    /**
     * 游戏结束ViewModel
     */
    public viewModel: GameOverViewModel = new GameOverViewModel();
    /**
     * 游戏结束UiState
     */
    public uiState: GameOverUiState = this.viewModel.uiState;

    onLoad() {
        // 初始化界面状态
        this.uiState.bgNode = this.node.getChildByName("BG");
        this.uiState.bgAnimation = this.uiState.bgNode.getComponent(Animation);
        this.uiState.scoreLabel = this.node.getChildByName("Score").getComponent(Label);
    }

    onEnable() {
        // 播放背景动画
        this.uiState.bgAnimation.play("game_over_bg_fade_in");

        // 设置分数
        this.uiState.scoreLabel.string = `${AriPlane.SCORE}`;
    }

    start() {

    }

    update(deltaTime: number) {

    }

    /**
     * 再玩一局点击事件
     */
    public onReplay() {
        // 播放点击音效
        AudioManager.getInstance().playSound(AudioPath.clickSound);

        // 隐藏游戏结束面板
        EventManager.emit(EventType.MENU_VISIBILITY, NodePath.OVER_MENU, false);
        // 显示游戏面板
        EventManager.emit(EventType.MENU_VISIBILITY, NodePath.GAME_MENU, true);

        // 创建玩家
        EventManager.emit(EventType.CREATE_PLAYER);
    }

    /**
     * 返回主菜单点击事件
     */
    public onBack() {
        // 播放点击音效
        AudioManager.getInstance().playSound(AudioPath.clickSound);

        // 隐藏游戏结束面板
        EventManager.emit(EventType.MENU_VISIBILITY, NodePath.OVER_MENU, false);
        // 显示游戏主菜单面板
        EventManager.emit(EventType.MENU_VISIBILITY, NodePath.MAIN_MENU, true);
    }
}